package com.ibeeking.found.uaa.cs.service.impl;

import com.ibeeking.found.auth.api.common.dto.LoginUserDTO;
import com.ibeeking.found.auth.api.common.query.AccountLoginQuery;
import com.ibeeking.found.auth.api.feign.CsLoginClient;
import com.ibeeking.found.common.config.feign.MsgFeignClient;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.uaa.cs.model.CsUserDetails;
import com.ibeeking.found.uaa.cs.service.ICsUserDetailsService;
import com.ibeeking.nematos.exception.AuthorizationException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ibeeking
 */
@Service
public class CsUserDetailsServiceImpl implements ICsUserDetailsService {

    @Resource
    private CsLoginClient csLoginClient;
    @Resource
    private MsgFeignClient msgClient;

    @Override
    public CsUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机号不能为空");
        }
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(username);
        return queryCSLoginUser(query);
    }

    @Override
    public CsUserDetails loadUserByPhoneAndSmsCode(String phone, String smsCode, String key) {
        if (StringUtils.isBlank(phone)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机号不能为空");
        }
        if (StringUtils.isBlank(smsCode)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "手机验证码不能为空");
        }
        if (StringUtils.isBlank(key)) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码标识不能为空");
        }
        ResponseResult<Boolean> responseResult = msgClient.checkVerificationCode(key, phone, smsCode);
        if (null == responseResult || !responseResult.getData()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "验证码错误或已过期");
        }

        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(phone);
        return queryCSLoginUser(query);
    }

    /***
     * 封装查询登录用户
     * @param query 参数
     * @return
     */
    private CsUserDetails queryCSLoginUser(AccountLoginQuery query) {
        //查询商铺用户
        ResponseResult<LoginUserDTO> responseResult = csLoginClient.queryAccountLogin(query);
        if (null == responseResult || null == responseResult.getData() || !responseResult.getData().getJob()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "用户名或密码错误");
        }
        if (responseResult.getData().getLock()) {
            throw new AuthorizationException(HttpStatus.UNAUTHORIZED.value(), "用户已被锁定，禁止登录");
        }
        CsUserDetails csUserDetails = BeanUtil.convertBean(responseResult.getData(), CsUserDetails.class);
        csUserDetails.setUserCode(query.getMobile());
        csUserDetails.setTenantId(String.valueOf(responseResult.getData().getTenantId()));
        csUserDetails.setIndustry(responseResult.getData().getIndustry());
        List<SimpleGrantedAuthority> roles = new ArrayList<>();
        roles.add(new SimpleGrantedAuthority(LoginRoleEnum.CUSTOMER_SERVICE.name()));
        roles.add(new SimpleGrantedAuthority(LoginRoleEnum.CUSTOMER_SERVICE.name()));
        csUserDetails.setRoles(roles);
        return csUserDetails;
    }
}
